﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace QuanLyThuNgan
{
    public partial class frm_XacNhan : DevExpress.XtraEditors.XtraForm
    {
        public frm_XacNhan()
        {
            InitializeComponent();
        }
        DonVi_Khac dv_khac = new DonVi_Khac();
        ThuDTLDTO thudtl = new ThuDTLDTO();
        ThuDTLCTL thudata = new ThuDTLCTL();
        DataTable dtLoadDSPhieu = new DataTable();
        BenhNhanInfo bn = new BenhNhanInfo();
        DataTable dtkt = new DataTable();
        int row_dsbn = 0;
        int idcathungan;
        private void frm_BenhNhanNoiTru_Load(object sender, EventArgs e)
        {
            LoadDSTamUng();
            this.Focus();
            txtSoHoSo_DS.Focus();
        }
        private void LoadDSTamUng()
        {
            CaThuNganCTL ctl = new CaThuNganCTL();
            dtkt = ctl.m_SelectKiemTra(frmLogin.nd.IDNguoiDung);
            DataTable dtDonVi = dv_khac.DMDonVi();
            ClassHelp.m_SearchLookUpEdit(dtDonVi, searchLookUpEditKhoa, "TenDonVi", "MaDonVi");
            
        }
        private void btnTimKiem_DS_Click(object sender, EventArgs e)
        {
            if (txtHoTen_DS.Text == "" && txtSoHoSo_DS.Text == "")
                if (txtSoNhapVien_DS.Text == "" && searchLookUpEditKhoa.Text == "")
                    if(txtNamSinh_DS.Text =="" && txtTen.Text=="")
                       return;
            if (int.Parse(dtkt.Rows[0]["kq"].ToString()) == 1)
            {
              idcathungan = int.Parse(dtkt.Rows[0]["IDCaThuNgan"].ToString());
                //if(searchLookUpEditKhoa.EditValue.ToString()==         
                DataTable dtdsbenhnhan = dv_khac.TimKiemBNNoiTru_Ngoai(txtSoHoSo_DS.Text, txtHoTen_DS.Text, txtTen.Text,
                txtNamSinh_DS.Text == "" ? 0 : Uit.it_Parse.ToInteger(txtNamSinh_DS.Text), txtSoNhapVien_DS.Text,
                searchLookUpEditKhoa.Text == "" ? "" : searchLookUpEditKhoa.EditValue.ToString(), idcathungan, checkEdit_loaibn.Checked == true ? 1 : 2);
                gridControl_DSBN.DataSource = dtdsbenhnhan;
            }
            else
                XtraMessageBox.Show("Ban chưa đăng ký ca");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
           txtHoTen_DS.Text = "" ;
           txtSoHoSo_DS.Text = "";
           txtSoNhapVien_DS.Text = "";
           searchLookUpEditKhoa.EditValue = -1;
           txtNamSinh_DS.Text ="";
           txtTen.Text = "";
           thudtl.Ssonhapvien = "";
        }
        private void OpenTap(string NameTap)
        {
            for (int i = 0; i < xtraTabControlNoiTru.TabPages.Count; i++)
            {
                if (xtraTabControlNoiTru.TabPages[i].Name.Equals(NameTap))
                {
                    xtraTabControlNoiTru.TabPages[i].PageEnabled = true;
                    // xtraTabControlChiPhiNGTru.TabPages[i].Focus();
                }
                else
                    xtraTabControlNoiTru.TabPages[i].PageEnabled = false;
            }
        }

        private void xtraTabControlNoiTru_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (xtraTabControlNoiTru.SelectedTabPage == xtraTabPage_DSBN)
            {
                txtSoHoSo_DS.Focus();
            }
            if (xtraTabControlNoiTru.SelectedTabPage == xtraTabPage_XacNhan)
            {
                gridControlNhap.Focus();
                dtLoadDSPhieu = dv_khac.DSPhieuNop_Noi_Ngoai(thudtl.Ssohs, thudtl.Ssonhapvien,idcathungan,checkEdit_loaibn.Checked == true ? 1 : 2);
                gridControlNhap.DataSource = dtLoadDSPhieu;
                txtSoHoSo.Text = thudtl.Ssohs;
                txtHoTen.Text = bn.HoTen;
                txtGioiTinh.Text = bn.Gioi;
                txtNamSinh.Text = bn.NamSinh.ToString();
                txtChuanDoanNhapVien.Text = bn.DiaChi2;
                txtDiaChi.Text = bn.DiaChi;
                txtSoNhanVien.Text = thudtl.Ssonhapvien;
                txtNgayNhapVien.Text = bn.Ngaynhapvien;
                txtNgayXuatVien.Text = bn.Ngayxuatvien;
                txtTraiBenh.Text = bn.Traibenh;
                thudtl.Ssonhapvien = "";
            }
            
        }

        private void gridView_DSBN_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView_DSBN.GetDataRow(gridViewBNNTru.FocusedRowHandle);
            if (dr == null) return;
            thudtl.Ssohs = dr["SoHS"].ToString();
            thudtl.Ssonhapvien = dr["sonhapvien"].ToString();
            thudtl.Ssobn = "";
            bn.HoTen = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
            bn.Gioi = dr["GioiTinh"].ToString();
            bn.NamSinh = int.Parse(dr["NamSinh"].ToString());
            bn.DiaChi2 = dr["ChanDoan"].ToString();//DiaChi Thay Tam Cho Dia Chi
            bn.Ngaynhapvien = dr["ngaynhapvien"].ToString();
            bn.Ngayxuatvien = dr["ngayxuatvien"].ToString();
            bn.DiaChi = dr["DiaChi"].ToString();
            bn.Ngay = dr["ngay"].ToString();
            searchLookUpEditKhoa.EditValue = dr["traibenh"];
            bn.Traibenh = searchLookUpEditKhoa.Text;
            OpenTap(xtraTabPage_XacNhan.Name);
        }

        private void gridViewBNNTru_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView_DSBN.GetDataRow(gridView_DSBN.FocusedRowHandle);
            if (dr == null) return;
         
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gridViewBNNTru.RowCount; i++)
            {
                if ((bool)gridViewBNNTru.GetRowCellValue(i, "chon"))
                    dv_khac.Update_Thudlt_BafeeMst(gridViewBNNTru.GetRowCellValue(i, "sobn").ToString(), checkEdit_loaibn.Checked == true ? 1 : 2);
            }
        }

        private void gridView_DSBN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                DataRow dr = gridView_DSBN.GetDataRow(row_dsbn);
                thudtl.Ssohs = dr["SoHS"].ToString();
                thudtl.Ssonhapvien = dr["sonhapvien"].ToString();
                thudtl.Ssobn = "";
                bn.HoTen = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
                bn.Gioi = dr["GioiTinh"].ToString();
                bn.NamSinh = Uit.it_Parse.ToInteger(dr["NamSinh"]);
                bn.DiaChi2 = dr["chandoan"].ToString();//DiaChi Thay Tam Cho Dia Chi
                bn.Ngaynhapvien = dr["ngaynhapvien"].ToString();
                bn.Ngayxuatvien = dr["ngayxuatvien"].ToString();
                bn.DiaChi = dr["DiaChi"].ToString();
                bn.Ngay = dr["ngay"].ToString();
                searchLookUpEditKhoa.EditValue = dr["traibenh"];
                bn.Traibenh = searchLookUpEditKhoa.Text;
                OpenTap(xtraTabPage_XacNhan.Name);
            }

        }

        private void gridView_DSBN_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            row_dsbn = e.FocusedRowHandle;
        }

        private void btnDSBNKhac_Click(object sender, EventArgs e)
        {
            OpenTap(xtraTabPage_DSBN.Name);
        }

        private void btnLuuIn_Click(object sender, EventArgs e)
        {
          
            
        }

        private void xtraTabControlNoiTru_Click(object sender, EventArgs e)
        {

        }

        private void gridViewBNNTru_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle < 0)
                    e.Info.DisplayText = "";
                else
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
            else
                e.Info.DisplayText = "STT";
            e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
        }

        private void gridControlNhap_KeyDown(object sender, KeyEventArgs e)
        {
            ColumnView View = (ColumnView)gridControlNhap.FocusedView;
            GridColumn column = View.Columns["chon"];
            if (column != null)
            {
                View.FocusedColumn = column;
            }
        }

        private void gridControlNhap_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {

                GridControl grid = sender as GridControl;

                GridView view = grid.FocusedView as GridView;

                if ((e.Modifiers == Keys.None && view.IsLastRow && view.FocusedColumn.VisibleIndex == view.VisibleColumns.Count - 1)

                    || (e.Modifiers == Keys.Shift && view.IsFirstRow && view.FocusedColumn.VisibleIndex == 0))
                {

                    if (view.IsEditing)

                        view.CloseEditor();

                    SelectNextControl(grid, e.Modifiers == Keys.None, false, false, true);

                    e.Handled = true;
                    this.btnSave.Focus();
                }

            }
        }

    }
}